package com.lwj.interceptor;

import com.lwj.annotation.SensitiveField;
import com.lwj.utils.EncryptUtils;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.stereotype.Component;

import java.lang.reflect.Field;
import java.util.Objects;
@Component
public class AESDecrypt implements DecryptUtil {
    private String encryptKey="盐";

    /**
     * 解密
     *
     * @param result resultType的实例
     * @return T
     * @throws IllegalAccessException 字段不可访问异常
     */
    @Override
    public <T> T decrypt(T result) throws IllegalAccessException {
        //取出resultType的类
        Class<?> resultClass = result.getClass();
        Field[] declaredFields = resultClass.getDeclaredFields();
        for (Field field : declaredFields) {
            //取出所有被EncryptDecryptField注解的字段
            SensitiveField sensitiveField = field.getAnnotation(SensitiveField.class);
            if (!Objects.isNull(sensitiveField)) {
                field.setAccessible(true);
                Object object = field.get(result);
                //只支持String的解密
                if (object instanceof String&&object!=null) {
                    String value = (String) object;
                    //对注解的字段进行逐一解密
                    value = EncryptUtils.DESdecode(value, encryptKey);
                    field.set(result, value);
                }
            }
        }
        return result;
    }
}
